package cn.iocoder.yudao.module.worker.service.clerkfans;

import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import cn.iocoder.yudao.module.worker.controller.app.clerkfans.vo.AppClerkFansPageReqVO;
import cn.iocoder.yudao.module.worker.controller.app.clerkfans.vo.AppClerkFansSaveReqVO;
import cn.iocoder.yudao.module.worker.dal.mysql.clerk.ClerkMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import cn.iocoder.yudao.module.worker.controller.admin.clerkfans.vo.*;
import cn.iocoder.yudao.module.worker.dal.dataobject.clerkfans.ClerkFansDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.worker.dal.mysql.clerkfans.ClerkFansMapper;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.worker.enums.ErrorCodeConstants.*;

/**
 * 店员关注 Service 实现类
 *
 * @author Jerry
 */
@Service
@Validated
public class ClerkFansServiceImpl implements ClerkFansService {

    @Resource
    private ClerkFansMapper clerkFansMapper;

    @Resource
    private ClerkMapper clerkMapper;

    @Override
    public Long createClerkFans(ClerkFansSaveReqVO createReqVO) {
        // 插入
        ClerkFansDO clerkFans = BeanUtils.toBean(createReqVO, ClerkFansDO.class);
        clerkFansMapper.insert(clerkFans);
        // 返回
        return clerkFans.getId();
    }

    @Override
    public void updateClerkFans(ClerkFansSaveReqVO updateReqVO) {
        // 校验存在
        validateClerkFansExists(updateReqVO.getId());
        // 更新
        ClerkFansDO updateObj = BeanUtils.toBean(updateReqVO, ClerkFansDO.class);
        clerkFansMapper.updateById(updateObj);
    }

    @Override
    public void deleteClerkFans(Long id) {
        // 校验存在
        validateClerkFansExists(id);
        // 删除
        clerkFansMapper.deleteById(id);
    }

    private void validateClerkFansExists(Long id) {
        if (clerkFansMapper.selectById(id) == null) {
            throw exception(CLERK_FANS_NOT_EXISTS);
        }
    }

    @Override
    public ClerkFansDO getClerkFans(Long id) {
        return clerkFansMapper.selectById(id);
    }

    @Override
    public PageResult<ClerkFansDO> getClerkFansPage(ClerkFansPageReqVO pageReqVO) {
        return clerkFansMapper.selectPage(pageReqVO);
    }

    @Override
    public Long createClerkFans(AppClerkFansSaveReqVO createReqVO) {
        // 插入
        ClerkFansDO clerkFansDO = clerkFansMapper.getClerkFans(createReqVO.getUserId(), createReqVO.getWorkerClerkId());
        if(clerkFansDO == null){
            clerkFansDO = BeanUtils.toBean(createReqVO, ClerkFansDO.class);
            clerkFansMapper.insert(clerkFansDO);
        }else{
            clerkFansMapper.deleteById(clerkFansDO);
        }

        Integer fansNum = clerkFansMapper.getClerkFansCount(createReqVO.getWorkerClerkId());
        clerkMapper.updateFansCount(createReqVO.getWorkerClerkId(), fansNum);
        // 返回
        return clerkFansDO.getId();
    }

    @Override
    public void updateClerkFans(AppClerkFansSaveReqVO updateReqVO) {
        // 校验存在
        validateClerkFansExists(updateReqVO.getId());
        // 更新
        ClerkFansDO updateObj = BeanUtils.toBean(updateReqVO, ClerkFansDO.class);
        clerkFansMapper.updateById(updateObj);
    }

    @Override
    public PageResult<ClerkFansDO> getClerkFansPage(AppClerkFansPageReqVO pageReqVO) {
        return clerkFansMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ClerkFansDO> getClerkFansList(Long userId, Set<Long> clerkIds) {
        if(userId == null || CollectionUtils.isAnyEmpty(clerkIds)){
            return CollectionUtils.newArrayList(new ArrayList<>());
        }
        return clerkFansMapper.getClerkFansList(userId, clerkIds);
    }

}